<?php
/**
 * @author tungck
 * @filesource luongthuong.php
 * quan ly luong thường
*/

class Luongthuong extends CI_Controller{
    
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }
    
    function danh_sach_luong_thuong(){
        $title = "Danh sách lương thưởng";
        $this->adminlayout->view("admin/luongthuong",null ,$title);        
    }
    
    function lay_danh_sach_luong_thuong(){
        try{
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/luongthuong_model");
        $result = $this->luongthuong_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
        }catch(Exception $ex){
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Records'] = 'afhkahfkafhkahfakfhka';
            print json_encode($jTableResult);
        }
    }
    
    
    function them_luong_thuong(){
        
        $data['Thang'] = $_POST['Thang'];
        $data['Nam'] = $_POST['Nam'];
        $data['Luong'] = $_POST['Luong'];
        $data['Thuong'] = $_POST['Thuong'];
        $data['KPI'] = $_POST['KPI'];
        $data['MaHoSo'] = $_POST['MaHoSo'];
        $this->load->model("admin/luongthuong_model");
        $result = $this->luongthuong_model->them_luong_thuong($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
		$jTableResult['Record'] = $result;
		print json_encode($jTableResult);
    }
    
    function xoa_luong_thuong(){
        try{
        if(isset($_POST["ID"])){
            $id = $_POST["ID"];
            $this->load->model("admin/luongthuong_model");
            $this->luongthuong_model->xoa_luong_thuong($id);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function maHoSo(){
        $this->load->model("admin/staff_managers_model");
        $result = $this->staff_managers_model->getAll();
        $jTableResult = array();
        $rows = array();
        foreach($result as $row)
        {
            $rows[]=array("Value"=>$row->MaHoSo,"DisplayText"=>$row->MaHoSo);

        }

		$jTableResult['Result'] = "OK";
        $jTableResult['Options'] = $rows;
        print json_encode($jTableResult);
    }
    
    function test(){
        $filename=$_FILES["excel"]["tmp_name"]; 
        require_once (APPPATH."third_party/excel-reader/excel_reader2.php");   
        $uploaddir = APPPATH.'uploads/';
        $uploadfile = $uploaddir . basename($_FILES['excel']['name']);

        if (move_uploaded_file($_FILES['excel']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.\n";
        } else {
            echo "Possible file upload attack!\n";
        }
        $filename = $uploadfile; 
        $data = new Spreadsheet_Excel_Reader($filename,true,"UTF-8");   
        $rowsnum = $data->rowcount($sheet_index=0);  
        $colsnum =  $data->colcount($sheet_index=0);  
        for ($i=2;$i<=$rowsnum;$i++) 
            echo "<br>".$data->val($i,1)." , ".$data->val($i,2);             

    }
    
    
    function thay_doi_luong_thuong(){
        
        $data['ID'] = $_POST['ID'];
        $data['Thang'] = $_POST['Thang'];
        $data['Nam'] = $_POST['Nam'];
        $data['Luong'] = $_POST['Luong'];
        $data['Thuong'] = $_POST['Thuong'];
        $data['KPI'] = $_POST['KPI'];
        $data['MaHoSo'] = $_POST['MaHoSo'];
        $this->load->model("admin/luongthuong_model");
        $this->luongthuong_model->thay_doi_luong_thuong($data);
        //Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		print json_encode($jTableResult);
    }
    
}

?>